package com.firefly.service.query.ums;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.assembler.ums.DepartmentAssembler;
import com.firefly.dao.dao.ums.po.AdminPo;
import com.firefly.dao.dao.ums.po.DepartmentAdminPo;
import com.firefly.dao.dao.ums.po.DepartmentPo;
import com.firefly.dao.dao.ums.service.AdminService;
import com.firefly.dao.dao.ums.service.DepartmentAdminService;
import com.firefly.dao.dao.ums.service.DepartmentService;
import com.firefly.dao.vo.ums.DepartmentTreeVo;
import com.firefly.dao.vo.ums.DepartmentVo;
import com.firefly.util.utils.TreeNodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 部门查询
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/11/30
 **/
@Service
public class DepartmentQuery {
    private final DepartmentService departmentService;
    private final DepartmentAdminService departmentAdminService;
    private final DepartmentAssembler departmentAssembler;
    private final AdminService adminService;
    @Autowired
    public DepartmentQuery(DepartmentService departmentService,DepartmentAdminService departmentAdminService,DepartmentAssembler departmentAssembler,AdminService adminService){
        this.departmentService = departmentService;
        this.departmentAdminService = departmentAdminService;
        this.departmentAssembler = departmentAssembler;
        this.adminService = adminService;
    }
    // 获取所有部门
    public List<Map> tree() {
        QueryWrapper<DepartmentPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id").eq("del_flag",0);
        List<DepartmentPo> list = departmentService.list(queryWrapper);
        // 类型转换
        List<DepartmentTreeVo> listTree = departmentAssembler.getTreeList(list);
        //树形结构数据生成
        List<Map> result = TreeNodeUtils.toTree(0, listTree.stream().collect(Collectors.toList()));
        return result;
    }

    public List<Map> select() {
        QueryWrapper<DepartmentPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id").select("id","parent_id","name").eq("del_flag",0);
        List<DepartmentPo> list = departmentService.list(queryWrapper);
        //树形结构数据生成
        List<Map> result = TreeNodeUtils.toSelect(0, list.stream().collect(Collectors.toList()));
        return result;
    }
    public List<DepartmentVo> getList() {
        // 查出所有菜单来
        QueryWrapper<DepartmentPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id").eq("status", 1).eq("del_flag",0);
        List<DepartmentPo> list = departmentService.list(queryWrapper);
        return departmentAssembler.getListVo(list);
    }

    public DepartmentVo detail(Integer id){
        DepartmentPo department = departmentService.getById(id);
        return departmentAssembler.getVo(department);
    }
    // 下属用户
    public List<AdminPo> getAdmins(int id){
        QueryWrapper<DepartmentAdminPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", id);
        queryWrapper.select("admin_id");
        List<Object> list = departmentAdminService.listObjs(queryWrapper);
        QueryWrapper<AdminPo> queryAdmins = new QueryWrapper<>();
        queryAdmins.in(list.size() > 0, "id", list).lt(list.size() <= 0, "id", 0);
        List<AdminPo> admins = adminService.list(queryAdmins);
        return admins;
    }
}
